﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TfsAlert.Core;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using TfsAlert.Core.WorkflowServices;
using System.Threading;
using TfsAlert.Workflows;

namespace Microsoft.Services.TfsAlert.TfsAlert.ScenarioTests
{
    [TestClass]
    public class RelayWorkflow_UnitTests
    {
        static RelayService relayService;
        static AutoResetEvent _waitHandle = new AutoResetEvent ( false );
        
        [TestMethod]
        public void CanActivateRelay()
        {
            relayService = new RelayService( 3 );

            using ( WorkflowRuntime wfRuntime = WorkflowFactory.GetWorkflowRuntime() )
            {
                ExternalDataExchangeService dataService = new ExternalDataExchangeService();
                wfRuntime.AddService( dataService );
                dataService.AddService( relayService );

                WorkflowInstance wfInstance = null;

                int loopcount = 0;
                wfRuntime.WorkflowIdled += delegate( object sender, WorkflowEventArgs e )
                {
                    loopcount++;

                    if ( loopcount == 4 )
                    {
                        relayService.RelayOff();
                        _waitHandle.Set();
                    }
                };
                
                wfInstance = wfRuntime.CreateWorkflow( typeof( RS232 ) );
                wfInstance.Start();

                //Communicate with...

                relayService.InstanceId = wfInstance.InstanceId;
                relayService.RelayOn();

                _waitHandle.WaitOne();
                wfRuntime.StopRuntime();
            }

        }
    }
}
